
#setwd()

Sys.setenv(TZ='GMT')
options(scipen=999)


require(broom)
require(dotwhisker)
require(tidyr)
require(pastecs)
require(stargazer)
require(foreign)
require(dplyr)
require(ggplot2)
require(stringr)
require(devtools) # only if using github to get VDEM data
require(estimatr)
require(lfe)
require(lubridate)


# Enumerator Survey Data --------------------------------------------------

df<-read.csv("Enumerators_Data_SensitiveQs.csv")
# Afrobarometer Data ------------------------------------------------------

#This file requires the merged multi-country data sets from Afrobarometer Rounds 5-7
#Available at www.afrobarometer.org

# This code recodes the variables for each round of Afrobarometer since the questions change across rounds

## recode for Afrobarometer R5

#afro5<- #read in Afrobarometer Round 5 data


afro5$dateintr<-format(as.Date(afro5$dateintr, format="%d-%b-%y"), "%Y-%m-%d")

afro5<- afro5 %>% 
  mutate(refuse_bribe_schools=ifelse(q61e==998, 1, 0),
         refuse_bribe_clinic=ifelse(q61c==998, 1, 0),
         refuse_bribe_doc=ifelse(q61a==998,1,0),
         refuse_bribe_police=ifelse(q61d==998, 1, 0),
         refuse_bribe=ifelse(refuse_bribe_schools==1 | refuse_bribe_clinic==1 | refuse_bribe_doc==1 | refuse_bribe_police==1, 1, 0),
         refuse_trust_pres=ifelse(q59a==997, 1, 0),
         #pid
         refuse_pid1=ifelse(q89a==8, 1, 0),
         refuse_pid2=ifelse(q89b==9998, 1, 0),
         refuse_pid=ifelse(refuse_pid1==1 | refuse_pid2==1, 1, 0),
         refuse_elec=ifelse(q99==9998, 1, 0),
         # violence
         refuse_viol_elec=ifelse(q54==998, 1, 0),
         refuse_viol_unsafe=ifelse(q9a==998, 1, 0),
         refuse_viol_attack=ifelse(q10b==998, 1, 0),
         round=5,
         #   identity      
         refuse_eth=ifelse(q84==9998, 1, 0),
         refuse_eth=ifelse(q84==9997, NA, refuse_eth), 
         refuse_relig=ifelse(q98a==9998, 1, 0),
         
         ## IDK+refuse
         refuse_idk_bribe_schools=ifelse(q61e==998 |q61e==9 , 1, 0),
         refuse_idk_bribe_clinic=ifelse(q61c==998|q61c==9, 1, 0),
         refuse_idk_bribe_doc=ifelse(q61a==998|q61a==9,1,0),
         refuse_idk_bribe_police=ifelse(q61d==998|q61d==9, 1, 0),
         refuse_idk_bribe=ifelse(refuse_idk_bribe_schools==1 | refuse_idk_bribe_clinic==1 | refuse_idk_bribe_doc==1 | refuse_idk_bribe_police==1, 1, 0),
         refuse_idk_trust_pres=ifelse(q59a==997|q59a==9, 1, 0),
         #pid
         refuse_idk_pid1=ifelse(q89a==8|q89a==9, 1, 0),
         refuse_idk_pid2=ifelse(q89b==9998|q89b==999, 1, 0),
         refuse_idk_pid=ifelse(refuse_idk_pid1==1 | refuse_idk_pid2==1, 1, 0),
         refuse_idk_elec=ifelse(q99==9998|q99==9999, 1, 0),
         # violence
         refuse_idk_viol_elec=ifelse(q54==998|q54==9, 1, 0),
         refuse_idk_viol_unsafe=ifelse(q9a==998|q9a==9, 1, 0),
         refuse_idk_viol_attack=ifelse(q10b==998|q10b==9, 1, 0),
         
         #   identity      
         refuse_idk_eth=ifelse(q84==9998 | q84==9999, 1, 0),
         refuse_idk_eth=ifelse(q84==9997, NA, refuse_eth), 
         refuse_idk_relig=ifelse(q98a==9998 | q98a==9999, 1, 0),
         ## idk only
         
         idk_bribe_schools=ifelse(q61e==9 , 1, 0),
         idk_bribe_clinic=ifelse(q61c==9, 1, 0),
         idk_bribe_doc=ifelse(q61a==9,1,0),
         idk_bribe_police=ifelse(q61d==9, 1, 0),
         idk_bribe=ifelse(idk_bribe_schools==1 | idk_bribe_clinic==1 | idk_bribe_doc==1 | idk_bribe_police==1, 1, 0),
         idk_trust_pres=ifelse(q59a==9, 1, 0),
         #pid
         idk_pid1=ifelse(q89a==9, 1, 0),
         idk_pid2=ifelse(q89b==999, 1, 0),
         idk_pid=ifelse(idk_pid1==1 | idk_pid2==1, 1, 0),
         idk_elec=ifelse(q99==9999, 1, 0),
         # violence
         idk_viol_elec=ifelse(q54==9, 1, 0),
         idk_viol_unsafe=ifelse(q9a==9, 1, 0),
         idk_viol_attack=ifelse(q10b==9, 1, 0),
         
         #   identity      
         idk_eth=ifelse(q84==9999, 1, 0),
         idk_eth=ifelse(q84==9997, NA, refuse_eth), 
         idk_relig=ifelse(q98a==9999, 1, 0),
         
         # who sent us
         sent_govt=ifelse(q100==1, 1, 0),
         sent_pp=ifelse(q100==18, 1, 0),
         
         #people present
         present=ifelse(q106>1, 1, 0),
         
         
         COUNTRY=factor(country_alpha, levels=c(1,2,3,4,5,6,7,8,9,11,12,
                                                13,14,15,16,17,18,19,20,21,22,23,
                                                24,25,26,27,28,29,30,31,32,33,34,35), labels=c("Algeria", "Benin", "Botswana", "Burkina Faso",
                                                                                               "Burundi", "Cameroon", "Cabo Verde","Côte d'Ivoire", 
                                                                                               "Egypt", "Ghana", "Guinea", "Kenya", "Lesotho", 
                                                                                               "Liberia", "Madagascar", "Malawi", "Mali", "Mauritius", 
                                                                                               "Morocco", "Mozambique", "Namibia", "Niger", "Nigeria", 
                                                                                               "Senegal", "Sierra Leone", "South Africa","Sudan", 
                                                                                               "eSwatini", "Tanzania", "Togo", "Tunisia", 
                                                                                               "Uganda", "Zambia", "Zimbabwe")), 
         N=1,
         edu=ifelse(q97<=9,q97, NA)) %>%  
  #drop islands
  filter(COUNTRY!="eSwatini") %>% filter(COUNTRY!="Cabo Verde") %>% filter(COUNTRY!="São Tomé and Príncipe") %>% filter(COUNTRY!="Mauritius") %>%
  #drop countries not included in R7 
  filter(COUNTRY!="Algeria") %>% filter(COUNTRY!="Burundi") %>% filter(COUNTRY!="Egypt")

afro5<- afro5 %>% select(dateintr, COUNTRY,round, N,  colnames(afro5)[grepl("^idk",colnames(afro5))],colnames(afro5)[grepl("refuse",colnames(afro5))], 
                          colnames(afro5)[grepl("sent",colnames(afro5))], q101, edu)


## Recode for Afrobarometer R6

#afro6<- #read in Afrobarometer Round 6 data

afro6$dateintr<-format(as.Date(afro6$dateintr, format="%d-%b-%y"), "%Y-%m-%d")

afro6<-afro6 %>% 
  mutate(refuse_bribe_schools=ifelse(q55b==98, 1, 0),
         refuse_bribe_clinic=ifelse(q55d==98, 1, 0),
         refuse_bribe_doc=ifelse(q55f==98,1,0),
         refuse_bribe_police=ifelse(q55j==98, 1, 0),
         refuse_bribe=ifelse(refuse_bribe_schools==1 | refuse_bribe_clinic==1 | refuse_bribe_doc==1 | refuse_bribe_police==1, 1, 0),
         refuse_trust_pres=ifelse(q52a==98, 1, 0),
         #pid
         refuse_pid1=ifelse(q90a==8, 1, 0),
         refuse_pid2=ifelse(q90b==9998, 1, 0),
         refuse_pid=ifelse(refuse_pid1==1 | refuse_pid2==1, 1, 0),
         refuse_elec=ifelse(q99==9998, 1, 0),
         # violence
         refuse_viol_elec=ifelse(q49==98, 1, 0),
         refuse_viol_unsafe=ifelse(q10a==98, 1, 0),
         refuse_viol_attack=ifelse(q11b==98, 1, 0),
         round=6,
         
         #   identity      
         refuse_eth=ifelse(q87==9998, 1, 0),
         refuse_eth=ifelse(q87==9997, NA, refuse_eth), 
         refuse_relig=ifelse(q98a==9998, 1, 0),
         ## IDK+refuse
         refuse_idk_bribe_schools=ifelse(q55b==98|q55b==9, 1, 0),
         refuse_idk_bribe_clinic=ifelse(q55d==98|q55d==9, 1, 0),
         refuse_idk_bribe_doc=ifelse(q55f==98|q55d==9,1,0),
         refuse_idk_bribe_police=ifelse(q55j==98|q55d==9, 1, 0),
         refuse_idk_bribe=ifelse(refuse_idk_bribe_schools==1 | refuse_idk_bribe_clinic==1 | refuse_idk_bribe_doc==1 | refuse_idk_bribe_police==1, 1, 0),
         refuse_idk_trust_pres=ifelse(q52a==98|q52a==9, 1, 0),
         #pid
         refuse_idk_pid1=ifelse(q90a==8|q90a==9, 1, 0),
         refuse_idk_pid2=ifelse(q90b==9998|q90b==9999, 1, 0),
         refuse_idk_pid=ifelse(refuse_idk_pid1==1 | refuse_idk_pid2==1, 1, 0),
         refuse_idk_elec=ifelse(q99==9998|q99==9999, 1, 0),
         # violence
         refuse_idk_viol_elec=ifelse(q49==98|q49==9, 1, 0),
         refuse_idk_viol_unsafe=ifelse(q10a==98|q10a==9, 1, 0),
         refuse_idk_viol_attack=ifelse(q11b==98|q11b==9, 1, 0),
         
         #   identity      
         refuse_idk_eth=ifelse(q87==9998|q87==9999, 1, 0),
         refuse_idk_eth=ifelse(q87==9997, NA, refuse_eth), 
         refuse_idk_relig=ifelse(q98a==9998 | q98a==9999, 1, 0),
         ## idk only
         idk_bribe_schools=ifelse(q55b==9, 1, 0),
         idk_bribe_clinic=ifelse(q55d==9, 1, 0),
         idk_bribe_doc=ifelse(q55d==9,1,0),
         idk_bribe_police=ifelse(q55d==9, 1, 0),
         idk_bribe=ifelse(idk_bribe_schools==1 | idk_bribe_clinic==1 | idk_bribe_doc==1 | idk_bribe_police==1, 1, 0),
         idk_trust_pres=ifelse(q52a==9, 1, 0),
         #pid
         idk_pid1=ifelse(q90a==9, 1, 0),
         idk_pid2=ifelse(q90b==9999, 1, 0),
         idk_pid=ifelse(idk_pid1==1 | idk_pid2==1, 1, 0),
         idk_elec=ifelse(q99==9999, 1, 0),
         # violence
         idk_viol_elec=ifelse(q49==9, 1, 0),
         idk_viol_unsafe=ifelse(q10a==9, 1, 0),
         idk_viol_attack=ifelse(q11b==9, 1, 0),
         #   identity      
         idk_eth=ifelse(q87==9999, 1, 0),
         idk_eth=ifelse(q87==9997, NA, refuse_eth), 
         idk_relig=ifelse(q98a==9999, 1, 0),
         # who sent us
         sent_govt=ifelse(q100==8, 1, 0),
         sent_pp=ifelse(q100==7, 1, 0),
         
         #people present
         present=ifelse(q106>1, 1, 0),
         
         COUNTRY=factor(country, levels=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35, 36), labels=c(
           "Algeria",
           "Benin",
           "Botswana",
           "Burkina Faso",
           "Burundi",
           "Cameroon",
           "Cabo Verde",
           "Côte d'Ivoire",
           "Egypt",
           "Gabon",
           "Ghana",
           "Guinea",
           "Kenya",
           "Lesotho",
           "Liberia",
           "Madagascar",
           "Malawi",
           "Mali",
           "Mauritius",
           "Morocco",
           "Mozambique",
           "Namibia",
           "Niger",
           "Nigeria",
           "São Tomé and Príncipe",
           "Senegal",
           "Sierra Leone",
           "South Africa",
           "Sudan",
           "eSwatini",
           "Tanzania",
           "Togo",
           "Tunisia",
           "Uganda",
           "Zambia",
           "Zimbabwe")),
         N=1,
         edu=ifelse(q97<=9,q97, NA)) %>%  
  
  #drop islands
  filter(COUNTRY!="eSwatini") %>% filter(COUNTRY!="Cabo Verde") %>% filter(COUNTRY!="São Tomé and Príncipe") %>% filter(COUNTRY!="Mauritius") %>% 
  #drop countries not included in R7 
  filter(COUNTRY!="Algeria") %>% filter(COUNTRY!="Burundi") %>% filter(COUNTRY!="Egypt")

afro6<- afro6 %>% select(dateintr, COUNTRY,round, N,  colnames(afro6)[grepl("^idk",colnames(afro6))],colnames(afro6)[grepl("refuse",colnames(afro6))],colnames(afro6)[grepl("sent",colnames(afro6))], q101, edu)

## Recode for Afrobarometer R7

# afro<- #read in Afrobarometer Round 7 data
afro$dateintr<-as.character(afro$DATEINTR+ISOdate(1582,10,14))
afro$dateintr<-unlist(strsplit(afro$dateintr, split=" ", fixed=TRUE))[seq(1,45823*2,2)]
afro$dateintr<-as.Date(afro$dateintr)

afro<- afro %>% filter(COUNTRY!="eSwatini") %>% filter(COUNTRY!="Cabo Verde") %>% filter(COUNTRY!="São Tomé and Príncipe") %>% filter(COUNTRY!="Mauritius")
#Gabon, Gambia not in R5


#sensi questions
afro<-afro %>% mutate_at(c("Q49C", "Q49G", "Q49K", "Q49N", "Q49R", "Q49T", 
                           "Q43A", "Q88A", "Q88B", "Q99", "Q84", "Q98", "Q100", "Q106"), as.numeric) %>%
  mutate(round=7,
         refuse_bribe_schools=ifelse(Q49C==7, 1, 0),
         refuse_bribe_clinic=ifelse(Q49G==7, 1, 0),
         refuse_bribe_doc=ifelse(Q49K==7,1,0),
         refuse_bribe_service=ifelse(Q49N==7, 1, 0),
         refuse_bribe_police1=ifelse(Q49R==7, 1, 0),
         refuse_bribe_police2=ifelse(Q49T==7, 1, 0),
         refuse_bribe=ifelse(refuse_bribe_schools==1 | refuse_bribe_clinic==1 | refuse_bribe_doc==1 | refuse_bribe_service==1|refuse_bribe_police1==1 | refuse_bribe_police2==1, 1, 0),
         refuse_trust_pres=ifelse(Q43A==6, 1, 0),
         refuse_pid1=ifelse(Q88A==4, 1, 0),
         refuse_pid2=ifelse(Q88B==327, 1, 0),
         refuse_pid=ifelse(refuse_pid1==1 | refuse_pid2==1, 1, 0),
         refuse_elec=ifelse(Q99==350, 1, 0),
         
         #   identity      
         refuse_eth=ifelse(Q84==535, 1, 0),
         refuse_eth=ifelse(Q84==534, NA, refuse_eth), 
         refuse_relig=ifelse(Q98==72, 1, 0),
         
         # refuse + idk here
         refuse_idk_bribe_schools=ifelse(Q49C==7 | Q49C==8, 1, 0),
         refuse_idk_bribe_clinic=ifelse(Q49G==7|Q49G==8, 1, 0),
         refuse_idk_bribe_doc=ifelse(Q49K==7|Q49K==8,1,0),
         refuse_idk_bribe_service=ifelse(Q49N==7|Q49N==8, 1, 0),
         refuse_idk_bribe_police1=ifelse(Q49R==7|Q49R==8, 1, 0),
         refuse_idk_bribe_police2=ifelse(Q49T==7|Q49T==8, 1, 0),
         refuse_idk_bribe=ifelse(refuse_idk_bribe_schools==1 | refuse_idk_bribe_clinic==1 | refuse_idk_bribe_doc==1 | refuse_idk_bribe_service==1|refuse_idk_bribe_police1==1 | refuse_idk_bribe_police2==1, 1, 0),
         refuse_idk_trust_pres=ifelse(Q43A==6|Q43A==7, 1, 0),
         refuse_idk_pid1=ifelse(Q88A==4|Q88A==5, 1, 0),
         refuse_idk_pid2=ifelse(Q88B==327 | Q88B==328, 1, 0),
         refuse_idk_pid=ifelse(refuse_idk_pid1==1 | refuse_idk_pid2==1, 1, 0),
         refuse_idk_elec=ifelse(Q99==350 | Q99==351, 1, 0),
         #   identity      
         refuse_idk_eth=ifelse(Q84==535|Q84==536, 1, 0),
         refuse_idk_eth=ifelse(Q84==534, NA, refuse_eth), 
         refuse_idk_relig=ifelse(Q98==72 | Q98==73, 1, 0),
         #idks only
         idk_bribe_schools=ifelse( Q49C==8, 1, 0),
         idk_bribe_clinic=ifelse(Q49G==8, 1, 0),
         idk_bribe_doc=ifelse(Q49K==8,1,0),
         idk_bribe_service=ifelse(Q49N==8, 1, 0),
         idk_bribe_police1=ifelse(Q49R==8, 1, 0),
         idk_bribe_police2=ifelse(Q49T==8, 1, 0),
         idk_bribe=ifelse(idk_bribe_schools==1 | idk_bribe_clinic==1 | idk_bribe_doc==1 | idk_bribe_service==1|idk_bribe_police1==1 | idk_bribe_police2==1, 1, 0),
         idk_trust_pres=ifelse(Q43A==7, 1, 0),
         idk_pid1=ifelse(Q88A==5, 1, 0),
         idk_pid2=ifelse(Q88B==328, 1, 0),
         idk_pid=ifelse(idk_pid1==1 | idk_pid2==1, 1, 0),
         idk_elec=ifelse(Q99==351, 1, 0))

afro$N<-1

afro<-afro %>% mutate_at(c("Q40", "Q10A", "Q10B", "Q11A", "Q11B", "Q97"), as.numeric) %>%
  mutate(refuse_viol_elec=ifelse(Q40==6, 1, 0),
         refuse_viol_unsafe=ifelse(Q10A==7, 1, 0),
         refuse_viol_crime=ifelse(Q10B==7, 1, 0),
         refuse_viol_stole=ifelse(Q11A==6, 1, 0),
         refuse_viol_attack=ifelse(Q11B==6, 1, 0),
         #refuse + idk
         refuse_idk_viol_elec=ifelse(Q40==6|Q40==7, 1, 0),
         refuse_idk_viol_unsafe=ifelse(Q10A==7|Q10A==8, 1, 0),
         refuse_idk_viol_crime=ifelse(Q10B==7|Q10B==8, 1, 0),
         refuse_idk_viol_stole=ifelse(Q11A==6|Q11A==7, 1, 0),
         refuse_idk_viol_attack=ifelse(Q11B==6|Q11B==7, 1, 0),
         #idk only
         idk_viol_elec=ifelse(Q40==7, 1, 0),
         idk_viol_unsafe=ifelse(Q10A==8, 1, 0),
         idk_viol_crime=ifelse(Q10B==8, 1, 0),
         idk_viol_stole=ifelse(Q11A==7, 1, 0),
         idk_viol_attack=ifelse(Q11B==7, 1, 0),
         edu=ifelse(Q97<=11, Q97, NA),
         idk_eth=ifelse(Q84==536, 1, 0),
         idk_eth=ifelse(Q84==534, NA, refuse_eth), 
         idk_relig=ifelse(Q98==73, 1, 0),
         # who sent us
         sent_govt=ifelse(Q100==10, 1, 0),
         sent_pp=ifelse(Q100==9, 1, 0),
         #people present
         present=ifelse(Q106>2, 1, 0)) %>%
  #select(COUNTRY, round, N, sensi_q2, pid_q, violence_q, edu, Q101) %>%  
  #not in R5/6
  filter(COUNTRY!="Gabon") %>% filter(COUNTRY!="Gambia") 

afro7<- afro %>% select(dateintr, COUNTRY,round, N,  colnames(afro)[grepl("^idk",colnames(afro))],colnames(afro)[grepl("refuse",colnames(afro))], 
                         Q101, colnames(afro)[grepl("sent",colnames(afro))], edu)

afro7<- afro7 %>% rename(q101=Q101)
drop<-setdiff(names(afro7), names(afro6))

afro7<-afro7[,!colnames(afro7) %in% drop]
drop<-setdiff(names(afro6), names(afro7))

afro6<-afro6[,!colnames(afro6) %in% drop]
afro5<-afro5[,!colnames(afro5) %in% drop]

v<-intersect(names(afro7), names(afro6))

afro6<- afro6 %>% select(v)
afro5<- afro5 %>% select(v)
afro7<-afro7 %>% select(v)

afro<-rbind(afro5, afro6, afro7)

afro<- afro %>% mutate(COUNTRY=as.character(COUNTRY))
# removing countries where ethnicity wasn't asked
afro <- afro %>% mutate(refuse_eth=ifelse(COUNTRY=="Tunisia" | COUNTRY=="Sudan"|COUNTRY=="Morocco", NA, refuse_eth)) %>%   
  filter(COUNTRY!="Gabon") %>% filter(COUNTRY!="Gambia") 

# change Cote d'Ivoire so it doesn't have any unicode/errors
afro<- afro %>% mutate(COUNTRY=ifelse(COUNTRY=="Côte d'Ivoire", "Cote d'Ivoire", COUNTRY))

# VDEM Data --------------------------------------------------------------------


## # Download V-DEM data
# now, install the vdemdata package directly from GitHub
devtools::install_github("vdeminstitute/vdemdata")
# Or download from here: https://v-dem.net/data/the-v-dem-dataset/ 

vdem<- vdem %>% select(country_name, year, v2cacamps_ord, v2smpolsoc_ord, v2x_clphy, v2caviol) %>% 
  #limit to Afrobarometer years
  filter(year>=2015 & year<=2017) %>% 
  #change names to match afrobarometer
  mutate(country_name=ifelse(country_name=="Ivory Coast", "Cote d'Ivoire", 
                             ifelse(country_name=="Cape Verde", "Cabo Verde", 
                                    ifelse(country_name=="Sao Tome and Principe", 
                                           "São Tomé and Príncipe", 
                                           ifelse(country_name=="The Gambia", "Gambia", country_name))))) %>%
  filter(country_name!="Gabon")

#limit to afrobarometer countries
afro_vdem<-intersect(unique(afro$COUNTRY), vdem$country_name)

vdem <- vdem %>% filter( country_name %in% afro_vdem) %>% filter(complete.cases(.)) %>% 
  mutate(round=ifelse(year==2015, 5, ifelse(year==2016, 6, ifelse(year>=2017, 7, NA))))

names(vdem)<-c("COUNTRY", "year", "v2cacamps_ord", "v2smpolsoc_ord", "v2x_clphy", "v2caviol",   "round")
vdem$polarized<-ifelse(vdem$v2cacamps_ord>=3, 1, 0)




# Afrobarometer Censorship Index ------------------------------------------

# Creating PID censorship index from Afrobarometer data

pid_q<-c("refuse_pid", "refuse_elec")
sensi_q2<-c("refuse_bribe", "refuse_trust_pres")

sensi_p<-afro %>% group_by(COUNTRY, round) %>% summarise_at(c(pid_q, sensi_q2, "N"), sum, na.rm=T) 
sensi_p$sensi_index<- rowSums(sensi_p[pid_q], na.rm=T)
sensi_p$nonsensi_index<- rowSums(sensi_p[sensi_q2], na.rm=T)
sensi_p<- sensi_p %>% group_by(COUNTRY, round) %>% mutate(sensi_index=sensi_index/(length(pid_q)*N))
sensi_p<- sensi_p %>% group_by(COUNTRY, round) %>% mutate(nonsensi_index=nonsensi_index/(length(sensi_q2)*N))
sensi_p$censor_index<-sensi_p$sensi_index-sensi_p$nonsensi_index
sensi_p$country_year<-paste0(sensi_p$COUNTRY," - Round", sensi_p$round)

sensi_p$country_year2<-paste0("Round ", sensi_p$round)
sensi_p$censor_index_type<-"PID vs.\nSensitive Qs"
sensi_p<- sensi_p %>% select(COUNTRY, country_year2, censor_index, censor_index_type, round)

sensi_p<-merge(sensi_p, vdem, by=c("COUNTRY", "round"))
sensi_p$pol_polarized<-ifelse(sensi_p$v2cacamps_ord>=3, "Politically \npolarized", "Not politically \npolarized")
sensi_p$soc_polarized<-ifelse(sensi_p$v2smpolsoc_ord<=1, "Society polarized", "Not society polarized")


# Fig 2: Why enumerators feel uncomfortable -------------------------------

sensitive_pid<-c("sensitive_pid_uncomf","sensitive_pid", 
                 colnames(df)[grepl("sensitive_pid_why_",colnames(df))][1:7], 
                 "sensitive_pid_percen_1", colnames(df)[grepl("sensitive_pid_who_",colnames(df))])
sensitive_pid2<-c("Uncomfortable (PID) - dichtomous","Uncomfortable - PID", "Why-personal private", "why-reaction will affect answers", "why-culturally appropriate", "why-political agent",
                  "why-reveal my PID", "why-create tensions", "why-other",
                  "Percent answer honestly", "Sensitive-women","Sensitive-opposition", "Sensitive-education", "Sensitive-rural","sensitive-copartisans", "sensitive-rebel")

vars<-c("age", "dept_abidjan", "sex", "akan", "krou", "nmande", "smande", "voltaique", "christian", "edu", "govt",
        "experience_years", "politics", "N_DeptWork", "projectN")


var.names<-c("Age", "From Abidjan", "Male", "Akan", "Krou", "North MandC)", "South MandC)", "VoltaC/que", "Christian", "Education","Govt supporter", "Experience (years)", "Political Projects", "Number of depts worked", "Projects N")
sensitive_topics<-c(colnames(df)[grepl("sensitive_topics_", colnames(df))][1:9])
sensitive_topics2<-c("Nothing", "Abortion",
                     "Disease Status (e.g. HIV/AIDS, TB)",
                     "Trust in government",
                     "Corruption",
                     "Intimate partner violence",
                     "Attitudes towards immigrants",
                     "Protest or violence participation",
                     "Respondents' voting history")

why_chal<-c(colnames(df)[grepl("why_challenges_",colnames(df))][1:6])
df %>% mutate(sensitive_pol=ifelse(sensitive_pid_why_4==1 | sensitive_pid_why_5==1, 1, 0),
              sensitive_reax=ifelse(sensitive_pid_why_2==1|sensitive_pid_why_6==1, 1, 0)) %>%
  filter(sensitive_pid<=1) %>% select(sensitive_pol, sensitive_reax, sensitive_pid_why_1, 
                                      sensitive_pid_why_3) %>%
  summarise_all(mean, na.rm=T) %>%  
  pivot_longer(everything()) %>% mutate(value=value*100) %>%
  mutate(why=c("Perceived as political agent\nor revealing own PID",
                                               "Reaction will affect answers\n or create tensions",
                                               "It's personal",
                                               "It's culturally inappropriate")) %>%
  mutate(why=factor(why)) %>%
  
  ggplot(aes(x=reorder(factor(why),value) , y=value)) + 
  geom_bar( stat = "identity", width = .5, fill="gray34") +
  labs(x="", y="", fill="")+
  theme_bw()+ 
  guides(fill = guide_legend(reverse=T), title="")+
  geom_text(aes(label=paste0(round(value,0), "%")), 
            fontface="bold",hjust=.98, vjust=.5, color="white", size=5)+
  
  theme(text = element_text(size=20),
        axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        legend.position = "none")+
  coord_flip()+
  ggtitle("Of enumerators who feel uncomfortable\nasking about PID,why uncomfortable?")+ylim(0,100)
  
  

# Fig 3: Enumerator expectations of honesty asking about sensitive questions ----------------------------

hist<-df %>% select(sensitive_pid_percen_1, sensitive_viol_perce_1) %>% pivot_longer(sensitive_pid_percen_1:sensitive_viol_perce_1) %>% filter(!is.na(value)) %>%
  mutate(name2=ifelse(name=="sensitive_pid_percen_1", "Do you feel close to any \nparticular political party?",
                      ifelse(name=="sensitive_viol_perce_1","Did you witness violence in your\n community during the war?", NA)))

ggplot(hist) + geom_line(aes(x=value,linetype = as.factor(name2)), 
                         trim=TRUE, adjust=2.5, size=1, stat="density",alpha=.6)  + 
  guides(linetype=guide_legend(title=NULL)) + 
  theme_classic() + 
  theme(axis.title.x = element_text(size = 12, vjust = .25)) + 
  xlab("Self-censorship Index") + ylab("Density") + 
  geom_vline(xintercept = 0, linetype="dashed", size=.3) + 
  
  labs(y="Density", fill="",x="Thinking about your experience as an enumerator, \nwhat percent of respondents do you think will answer this question honestly?")+theme_bw()+
  theme(text=element_text(size=20),   legend.key = element_rect(fill = "white", colour = "black"),
        legend.box.background = element_rect(color="black", size=2),
        legend.position=c(0.2, 0.875)) + geom_rug(aes(x=value,linetype = as.factor(name2)), alpha=.6)



# Fig 4: Enumerator expectations of who will answer PID honestly -------------------------------



sensitive_who_honest<-as.data.frame(lapply(df[,names(df) %in% colnames(df)[grepl("_who_",colnames(df))]], function(x) ifelse(x>=3,1, 0)))

sensitive_who_honest %>% summarize(across(sensitive_pid_who_1:sensitive_pid_who_6, mean, na.rm=T)) %>% 
  pivot_longer(sensitive_pid_who_1:sensitive_pid_who_6) %>% mutate(value=value*100) %>% 
  mutate(var=str_split(name, "_", simplify=T)[,2], type=str_split(name,"_", simplify=T)[,4]) %>% 
  
  mutate(category=ifelse(name=="sensitive_pid_who_1"|name=="sensitive_pid_who_4"|
                           name=="sensitive_pid_who_3", "Demographic", "PID")) %>%
  filter(category=="PID")%>%
  
  ggplot(aes(x=reorder(factor(type), value), y=value, fill=forcats::fct_rev(var))) + 
  geom_bar( stat = "identity",  fill="gray34") +
  scale_x_discrete(breaks=c(1,2,3,4,5,6),labels=c("Women","Opposition","Educated","Rural",
                                                  "My copartisans","Under former\nrebel control"))+       guides(fill = guide_legend(reverse=T), title="")+
  labs(x="", y="% enumerators agree would be pretty/somewhat honest", fill="")+
  #facet_grid(rows=vars(category), switch = "y")+
  theme_bw()+ 
  guides(fill = guide_legend(reverse=T), title="")+
  geom_text(aes(label=paste0(round(value,0), "%")), 
            fontface="bold",hjust=.98, vjust=.5, color="white", size=5)+
  
  theme_bw() + 
  theme(text = element_text(size=20),
        axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        legend.position = "none",
        strip.placement = "outside") +
  coord_flip()+
  ggtitle("The following groups would be honest if you asked \nthem about PID...")+
  ylim(0,100)


ggsave("who_honest-PID.pdf", width = 11, height = 5)


# Figure 5: Afrobarometer Self-censorship index ---------------------------

ggplot(sensi_p, aes(y=forcats::fct_rev(COUNTRY), x=censor_index)) + 
  ylab("") + theme_bw() + 
  geom_point(size=2.5, alpha=.5)+
  facet_grid(~country_year2)+
  theme(text=element_text(size=25),legend.position = "none")  + 
  xlim(0, .23) +  xlab("Self-censorship Index, PID vs. Sensitive Qs")  


# Figure 6: Afrobarometer self-censorship index, by level of polar --------

ggplot(sensi_p) + geom_line(aes(x=censor_index, linetype=as.factor(pol_polarized)), 
                            trim=TRUE, adjust=2.5, size=1, stat="density",alpha=.6, color="gray34")  + 
  guides(color=F, linetype=guide_legend(title=NULL)) + theme_classic() +  
  theme(axis.title.x = element_text(size = 20, vjust = .25)) +
  xlab("Self-censorship Index, PID vs. Sensitive Qs") + ylab("Density")  + 
  scale_color_manual(values=c("grey50", "black"))+
  
  theme(text=element_text(size=20), legend.position=c(0.8, 0.875)) + 
  geom_rug(aes(x=censor_index,linetype=as.factor(pol_polarized)), alpha=.6,  color="gray34")+
  #CIV R5
  annotate("label", x = sensi_p$censor_index[grepl("Cote d'Ivoire", sensi_p$COUNTRY )& sensi_p$round==5], 
           y = 3.5,  label = "Cote d'Ivoire\n(R5)", size=5)+
  geom_segment(aes(x = sensi_p$censor_index[grepl("Cote d'Ivoire", sensi_p$COUNTRY)& sensi_p$round==5], 
                   y = 0, xend = sensi_p$censor_index[grepl("Cote d'Ivoire", sensi_p$COUNTRY)& sensi_p$round==5], 
                   yend = 3.1), linetype="dotted", color="grey50")+
  #CIV R6
  annotate("label", x = sensi_p$censor_index[grepl("Cote d'Ivoire", sensi_p$COUNTRY )& sensi_p$round==6], 
           y = 2.5, label = "Cote d'Ivoire\n(R6)", size=5)+
  geom_segment(aes(x = sensi_p$censor_index[grepl("Cote d'Ivoire", sensi_p$COUNTRY)& sensi_p$round==6], 
                   y = 0, xend = sensi_p$censor_index[grepl("Cote d'Ivoire", sensi_p$COUNTRY)& sensi_p$round==6], 
                   yend = 2.2), linetype="dotted", color="grey50")+
  #CIV R7 
  annotate("label", x = sensi_p$censor_index[grepl("Cote d'Ivoire", sensi_p$COUNTRY )& sensi_p$round==7], 
           y = 2.0, label = "Cote d'Ivoire\n(R7)", size=5)+
  geom_segment(aes(x = sensi_p$censor_index[grepl("Cote d'Ivoire", sensi_p$COUNTRY)& sensi_p$round==7], 
                   y = 0, xend = sensi_p$censor_index[grepl("Cote d'Ivoire", sensi_p$COUNTRY)& sensi_p$round==7], 
                   yend = 1.7), linetype="dotted", color="grey50")+
  #Zim R5
  annotate("label", x = sensi_p$censor_index[grepl("Zimbabwe", sensi_p$COUNTRY )& sensi_p$round==5], 
           y = 1.2, label = "Zimbabwe\n(R5)", size=5)+
  geom_segment(aes(x = sensi_p$censor_index[grepl("Zimbabwe", sensi_p$COUNTRY)& sensi_p$round==5], 
                   y = 0, xend = sensi_p$censor_index[grepl("Zimbabwe", sensi_p$COUNTRY)& sensi_p$round==5], 
                   yend = .9), linetype="dotted", color="grey50")+
  
  #Moz R6
  annotate("label", x = sensi_p$censor_index[grepl("Mozambique", sensi_p$COUNTRY )& sensi_p$round==6], 
           y = .2, label = "Mozambique\n(R6)", size=5)+
    xlim(0, .23)

# there will be warnings here, just ignore as they don't affect the creation of the plot

# Appendix Table A1: Demographic sum stats --------------------------------

vars<-c("age", "dept_abidjan", "sex", "akan", "krou", "nmande", "smande", 
        "voltaique", "christian", "edu", "govt","experience_years", 
        "politics", "N_DeptWork", "projectN")

var.names<-c("Age", "From Abidjan", "Male", "Akan", "Krou", "North Mandé", "South Mandé", 
             "Voltaïque", "Christian", "Education","Govt supporter", "Experience (years)", 
             "Political Projects", "Number of depts worked", "Projects N")

require(pastecs)
summary_stats<-t(stat.desc(df[vars]))

summary_stats<-as.data.frame(summary_stats[,c("mean", "min", "max", "std.dev", "nbr.val")])

summary_stats$desc<-var.names
summary_stats<-summary_stats[c(6,1,2,3,4,5)]
colnames(summary_stats)<-c("description", "mean", "min", "max", "std.dev", "n")

summary_stats  %>% mutate_if(is.numeric, round, 2) 

# Appendix Table A2: sum stats for Sensitive PID --------------------------------

sensitive_pid<-c("sensitive_pid_uncomf","sensitive_pid", colnames(df)[grepl("sensitive_pid_why_",colnames(df))][1:7], "sensitive_pid_percen_1", 
                 colnames(df)[grepl("sensitive_pid_who_",colnames(df))])
sensitive_pid2<-c("Uncomfortable (PID) - dichtomous","Uncomfortable - PID", "Why-personal private", "why-reaction will affect answers", 
                  "why-culturally appropriate", "why-political agent",
                  "why-reveal my PID", "why-create tensions", "why-other",
                  "Percent answer honestly", "Sensitive-women","Sensitive-opposition", "Sensitive-education", 
                  "Sensitive-rural","sensitive-copartisans", "sensitive-rebel")
require(pastecs)
sensitive_pid_stats<-t(stat.desc(df[sensitive_pid]))

sensitive_pid_stats<-as.data.frame(sensitive_pid_stats[,c("mean", "min", "max", "std.dev", "nbr.val")])
sensitive_pid_stats$des<-sensitive_pid2
sensitive_pid_stats<-sensitive_pid_stats[c(6,1,2,3,4,5)]
colnames(sensitive_pid_stats)<-c("description", "mean", "min", "max", "std.dev", "n")

sensitive_pid_stats  %>% mutate_if(is.numeric, round, 2) 

# Appendix Table A3: sum stats for sensitive topics --------------------------------

sensitive_topics<-c(colnames(df)[grepl("sensitive_topics_", colnames(df))][1:9])
sensitive_topics2<-c("Nothing", "Abortion",
                     "Disease Status (e.g. HIV/AIDS, TB)",
                     "Trust in government",
                     "Corruption",
                     "Intimate partner violence",
                     "Attitudes towards immigrants",
                     "Protest or violence participation",
                     "Respondents' voting history")

sensitive_topics_stat<-t(stat.desc(df[sensitive_topics]))

sensitive_topics_stat<-as.data.frame(sensitive_topics_stat[,c("mean", "min", "max", "std.dev", "nbr.val")])
sensitive_topics_stat$desc<-sensitive_topics2
sensitive_topics_stat<-sensitive_topics_stat[c(6,1,2,3,4,5)]
colnames(sensitive_topics_stat)<-c("description", "mean", "min", "max", "std.dev", "n")

sensitive_topics_stat %>% mutate_if(is.numeric, round, 2)

# Figure A1:Correlates of discomfort with questions --------------------------------

corre<- tidy(lm(sensitive_pid_uncomf~age+ dept_abidjan+ sex+ akan+ krou+ nmande+ smande+ voltaique+ christian+ edu+ govt+
                  experience_years+ politics+ N_DeptWork+ projectN, df))

corre<-bind_rows(corre) 


dwplot(corre, dot_args = list(size = 3),
       vline = geom_vline(
         xintercept = 0,
         colour = "grey60",
         linetype = 2
       )) %>% 
  relabel_predictors(
    c("(Intercept)"="Intercept",Violence_Experience_Any="Experienced Violence", challenges_5="Felt Unsafe",
      Violence_Witness_Any="Witnessed Violence", eth_work_noncoethnic_share="noncoethnic depts worked",pid_work_noncopart_share2="noncopartisan depts worked",
      work_sum_any_violence_share_x_abi="violent depts worked",experience_years="Experience",sex ="Male", 
      dept_abidjan="Abidjan",projectN="#projects",nmande ="North Mande",smande ="South Mande", krou ="Krou",
      voltaique="Voltaique", govt ="Govt", christian="Christian")
  )+
  theme_bw()+
  scale_color_manual(values=c("black" ),labels=c("Uncomfortable asking - PID"), guide = guide_legend(reverse = TRUE))+
  
  theme(axis.text = element_text(size=20), 
        legend.text = element_text(size=20), 
        legend.position = "bottom",
        legend.title = element_blank())


# Figure A2: % of respondents who would be honest --------------------------------

hist<-
  df %>% select(sensitive_pid_percen_1, sensitive_viol_perce_1) %>% pivot_longer(sensitive_pid_percen_1:sensitive_viol_perce_1) %>% filter(!is.na(value)) %>%
  mutate(name2=ifelse(name=="sensitive_pid_percen_1", "Do you feel close to any \nparticular political party?",
                      ifelse(name=="sensitive_viol_perce_1","Did you witness violence in your\n community during the war?", NA)))  %>% 
  mutate(perc25=ifelse(value<=25, "0-25%", 
                       ifelse(value>25 & value<=50, "26-50%",
                              ifelse(value>50 & value<=75, "51-75%",
                                     ifelse(value>75, "76-100%", NA)))),
         N=1) %>% filter(grepl("sensitive_pid", name))

hist2<- hist %>% group_by(name2, perc25) %>% summarise_at("N", sum, na.rm=T) %>% mutate(perc=N/214)



ggplot(hist2, aes(x=perc, y=name2, fill=forcats::fct_rev(factor(perc25))))+
  geom_bar(position = position_stack(), stat = "identity", width = .5, alpha=.7) +
  labs(y="", fill="",x="Share of enumerators reporting the percent of\nrespondents would be honest")+theme_bw()+
  theme(text=element_text(size=20))+    guides(fill = guide_legend(reverse=T))+scale_fill_grey()

# Table A4: Enumerator expectations of honesty by comfort asking about PID --------------------------------

df %>% filter(!is.na(sensitive_pid_uncomf)) %>% group_by(sensitive_pid_uncomf) %>% summarise_at(c("sensitive_pid_percen_1"), mean, na.rm=T)  %>% 
  mutate(sensitive_pid_uncomf=ifelse(sensitive_pid_uncomf==0, "Comfortable asking PID", "Uncomfortable asking PID"))  %>% 
  mutate_if(is.numeric, round, 2) %>% purrr::set_names(c("","% of respondents who would respond honestly"))


# Figure A3: why challenges~ % of respondents honest --------------------------------

why_chal_perc_pid_plot<-lapply(df[why_chal], function(x){tidy(lm(x~sensitive_pid_percen_1, df))})

why_chal_perc_pid_plot<-bind_rows(why_chal_perc_pid_plot) %>% filter(term=="sensitive_pid_percen_1")
why_chal_perc_pid_plot$term<-c("Nothing", "Timing", "Regions fielded", "Participants", "Who financed", "Topic")

why_chal_perc_pid_plot$model<-"pid"

why_chal_perc_violence_plot<-lapply(df[why_chal], function(x){tidy(lm(x~sensitive_viol_perce_1, df))})

why_chal_perc_violence_plot<-bind_rows(why_chal_perc_violence_plot) %>% filter(term=="sensitive_viol_perce_1")
why_chal_perc_violence_plot$term<-c("Nothing", "Timing", "Regions fielded", "Participants", "Who financed", "Topic")

why_chal_perc_violence_plot$model<-"violence"

why_chal_perc_plot<-rbind(why_chal_perc_pid_plot, why_chal_perc_violence_plot)

why_chal_perc_pid_plot %>% 
  dwplot(dot_args = list(size = 3),
         vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
         )) +
  theme_bw()+
  scale_color_manual(values=c( "black" ),labels=c( "% of respondents that would \nreply honestly - PID"), guide = guide_legend(reverse = TRUE))+
  theme(axis.text = element_text(size=20),
        legend.text = element_text(size=20),
        legend.position = "bottom",
        legend.title = element_blank())

# Figure A4: Political polarization --------------------------------
ggplot(vdem, aes(y = v2cacamps_ord, x = year)) +
  geom_bar(stat="identity", position=position_dodge())+ 
  theme_bw() + xlab("Afrobarometer Round Years")+ylab("Political Polarization")+
  facet_wrap(~COUNTRY)+
  #geom_text(aes(label=v2cacamps_ord), hjust=1.1, vjust=.5, color="white", size=4)+
  #geom_vline(xintercept=mean(vdem$v2cacamps_ord), linetype="dashed")+
  theme(text=element_text(size=15),
        #axis.title.x=element_blank(),
        #axis.text.x=element_blank(),
        #axis.ticks.x=element_blank(),
        legend.position="none",
        axis.text.y=element_text(size=15),
        axis.title.y = element_text(size=15),
        legend.text=element_text(size=15),
        legend.title = element_text(size=15))
# Table A6: Bivariate Regression: PID vs. Sensitive Qs Index, dichotomous polarization variable --------------------------------

ivs<-c("pol_polarized","soc_polarized")
lapply(sensi_p[ivs], function(x){tidy(lm_robust(censor_index~x, sensi_p))}) %>% 
  bind_rows() %>% filter(term!="(Intercept)") %>% 
  mutate_if(is.numeric, round, 2) %>% mutate(term=ivs)

# Table A7: Bivariate Regression: PID vs. Sensitive Qs Index, continuous outcomes --------------------------------
ivs<-c("v2cacamps_ord", "v2smpolsoc_ord")

lapply(sensi_p[ivs], function(x){tidy(lm_robust(censor_index~x, sensi_p))}) %>% bind_rows() %>% filter(term!="(Intercept)") %>% 
  mutate_if(is.numeric, round, 2) %>% mutate(term=ivs)

# Figure A5: Comparing Refuse and IDK, education and gender ---------------

idk_sensi_q2<-c("idk_bribe", "idk_trust_pres")

idk_pid_q<-c("idk_pid", "idk_elec")

idk_violence_q<-c("idk_viol_elec", "idk_viol_unsafe", "idk_viol_attack")
violence_q<-c("refuse_viol_elec", "refuse_viol_unsafe", "refuse_viol_attack")

afro$female<-ifelse(afro$q101=="Female" | afro$q101==2, 1, 0)

dvs_idk<-c(idk_pid_q, idk_violence_q, idk_sensi_q2)
dvs_refuse<-c(pid_q, violence_q, sensi_q2)

idk_edu<-lapply(afro[dvs_idk], function(x){tidy(lm_robust(x~edu, afro))}) %>% bind_rows() %>%
  mutate(model=term, term=rep(dvs_idk, each=2),  iv="IDK only")  %>% 
  filter(model!="(Intercept)") %>% mutate(term=factor(term, labels=c("PID", "Vote Choice", "Election Violence", "Unsafe", "Attacked", "Bribe", "Trust Prez")))

idk_gender<-lapply(afro[dvs_idk], function(x){tidy(lm_robust(x~female, afro))}) %>% bind_rows() %>%
  mutate(model=term, term=rep(dvs_idk, each=2), iv="IDK only")  %>% 
  filter(model!="(Intercept)")  %>% mutate(term=factor(term, labels=c("PID", "Vote Choice", "Election Violence", "Unsafe", "Attacked", "Bribe", "Trust Prez")))

idk<-rbind(idk_edu, idk_gender)

refuse_edu<-lapply(afro[dvs_refuse], function(x){tidy(lm_robust(x~edu, afro))}) %>% bind_rows() %>%
  mutate(model=term, term=rep(dvs_refuse, each=2),  iv="Refuse only") %>%
  filter(model!="(Intercept)")   %>% mutate(term=factor(term, labels=c("PID", "Vote Choice", "Election Violence", "Unsafe", "Attacked", "Bribe", "Trust Prez")))

refuse_gender<-lapply(afro[dvs_refuse], function(x){tidy(lm_robust(x~female, afro))}) %>% bind_rows() %>%
  mutate(model=term, term=rep(dvs_refuse, each=2), iv="Refuse only")  %>% 
  filter(model!="(Intercept)") %>% mutate(term=factor(term, labels=c("PID", "Vote Choice", "Election Violence", "Unsafe", "Attacked", "Bribe", "Trust Prez")))

idk_refuse_corre<-rbind(idk, refuse_edu, refuse_gender)

var=c(c("PID", "Vote Choice"),
      c("Election Violence", "Unsafe", "Attacked"),
      c("Bribe", "Trust Prez"))

idk_refuse_corre <- cbind(idk_refuse_corre, qs=c(rep("PID",2), rep("Violence",3), rep("Sensitive",2)), 
                          vars=rep(var, 2))

idk_refuse_corre<- idk_refuse_corre %>% mutate(vars2=factor(vars, level=var),
                                               attribute_ordered= factor(qs, levels = unique(qs)[c(1, 2, 3)]),
                                               ivs=factor(iv, level=unique(iv)))
width_d<-1

idk_refuse_corre <- idk_refuse_corre %>% filter(qs=="PID")

ggplot(idk_refuse_corre, aes(y = vars2, x = estimate, 
                             color=factor(model))) +
  #geom_text(aes(label=model), check_overlap = T,  position = position_dodge(width = width_d), vjust=1)+
  geom_point(aes(color=factor(model)), size = 4, position = position_dodge(width = width_d))+
  facet_grid(attribute_ordered~iv, scale="free_y", space = "free_y") +
  geom_errorbarh(aes(xmin = estimate - 1.96 * std.error, xmax = estimate + 1.96 * std.error), 
                 height = 0, alpha=.5, size=.75, position = position_dodge(width = width_d))+
  geom_errorbarh(aes(xmin = estimate - 1.64 * std.error, xmax = estimate + 1.64 * std.error), 
                 height = 0, size=1.25, position = position_dodge(width = width_d))+
  geom_vline(xintercept = 0, linetype = 2, color = "grey")+
  theme_bw() + #theme(text=element_text(size=15),legend.position = "none") + 
  scale_color_brewer(palette="Dark2")+
  ylab("")+
  guides(color = guide_legend(reverse=T))+
  theme(text=element_text(size=20),
        legend.title = element_blank(),
        legend.spacing.y = unit(5, 'cm'),
        legend.position = "bottom",
        panel.grid.major.y = element_blank(),
        legend.box.background = element_rect(colour = "black"))





# Figure A6: Self-censorship index (IDK + Refuse) ---------------


refuse_idk_sensi_q2<-c("refuse_idk_bribe", "refuse_idk_trust_pres")

refuse_idk_pid_q<-c("refuse_idk_pid", "refuse_idk_elec")

refuse_idk_violence_q<-c("refuse_idk_viol_elec", "refuse_idk_viol_unsafe", "refuse_idk_viol_attack")

sensi_p_idk<-afro %>% group_by(COUNTRY, round) %>% summarise_at(c(refuse_idk_pid_q, refuse_idk_sensi_q2, "N"), sum) 
sensi_p_idk$sensi_index<- rowSums(sensi_p_idk[refuse_idk_pid_q], na.rm=T)
sensi_p_idk$nonsensi_index<- rowSums(sensi_p_idk[refuse_idk_sensi_q2], na.rm=T)
sensi_p_idk<- sensi_p_idk %>% group_by(COUNTRY, round) %>% mutate(sensi_index=sensi_index/(length(refuse_idk_pid_q)*N))
sensi_p_idk<- sensi_p_idk %>% group_by(COUNTRY, round) %>% mutate(nonsensi_index=nonsensi_index/(length(refuse_idk_sensi_q2)*N))
sensi_p_idk$censor_index<-sensi_p_idk$sensi_index-sensi_p_idk$nonsensi_index

sensi_p_idk2<- sensi_p_idk %>% mutate(censor_index2=censor_index) %>% select(COUNTRY, censor_index2) %>% mutate(sensi="PID vs. Sensitive Qs")

sensi_p_idk$country_year2<-paste0(sensi_p_idk$COUNTRY," R", sensi_p_idk$round)


ggplot(sensi_p_idk, aes(y=reorder(country_year2,censor_index), x=censor_index)) + 
  #xlim(c(-.13,.13)) + 
  xlab("Self-censorship Index, \nPID vs. Sensitive Qs (IDKs and Refuse to Responds)") + 
  #geom_text(aes(label=round), hjust=-.1, vjust=.5, color="black", size=4)+
  ylab("") + scale_shape(solid = TRUE)+ theme_bw() + theme(text=element_text(size=15), 
                                                           #axis.text.y=element_blank(),
                                                           #axis.title.y =element_blank(),
                                                           #xis.ticks.y=element_blank(),
                                                           legend.title=element_blank(), legend.position="none")  + 
  geom_point(alpha=.5, color="gray34", size=2.5)























# Figure A7: Self-censorship index (IDK + Refuse), contexts ---------------

sensi_p_idk<-merge(sensi_p_idk, vdem, by=c("COUNTRY", "round"))

sensi_p_idk$pol_polarized<-ifelse(sensi_p_idk$v2cacamps_ord>=3, "Politically \npolarized", "Not politically \npolarized")
sensi_p_idk$soc_polarized<-ifelse(sensi_p_idk$v2smpolsoc_ord<=1, "society \npolarized", "Not society \npolarized")


ggplot(sensi_p_idk) + geom_line(aes(x=censor_index, linetype=as.factor(pol_polarized)), trim=TRUE, adjust=2.5, size=1, stat="density",alpha=.6, color="gray34")  + guides(color=F, linetype=guide_legend(title=NULL)) + theme_classic() +  theme(axis.title.x = element_text(size = 12, vjust = .25)) + xlab("Self-censorship Index, PID vs. Sensitive Qs") + ylab("Density")  + scale_color_manual(values=c("grey50", "black"))+
  
  theme(text=element_text(size=20), legend.position=c(0.8, 0.875)) + geom_rug(aes(x=censor_index,linetype=as.factor(pol_polarized)), alpha=.6,  color="gray34")


# Table A8: Bivariate Regression (IDKs + Refuse): PID vs. Sensiti --------
ivs<-c("pol_polarized","soc_polarized")
lapply(sensi_p_idk[ivs], function(x){tidy(lm_robust(censor_index~x, sensi_p_idk))}) %>% bind_rows() %>% filter(term!="(Intercept)") %>% 
  mutate_if(is.numeric, round, 2) %>% mutate(term=ivs) 

# Table A9: Bivariate Regression (IDKs + Refuse): PID vs. Sensiti --------

ivs<-c("v2cacamps_ord", "v2smpolsoc_ord")
lapply(sensi_p_idk[ivs], function(x){tidy(lm_robust(censor_index~x, sensi_p_idk))}) %>% bind_rows() %>% filter(term!="(Intercept)") %>% 
  mutate_if(is.numeric, round, 2) %>% mutate(term=ivs) 


# Figure A8: Self-censorship (identity) index by country (R5-7) -----------

id_q<-c("refuse_eth", "refuse_relig")

sensi_i<-afro %>% group_by(COUNTRY, round) %>% filter(!is.na(refuse_eth)) %>% summarise_at(c(pid_q, id_q, "N"), sum, na.rm=T) 

sensi_i$sensi_index<- rowSums(sensi_i[pid_q], na.rm=T)
sensi_i$nonsensi_index<- rowSums(sensi_i[id_q], na.rm=T)
sensi_i<- sensi_i %>% group_by(COUNTRY, round) %>% mutate(sensi_index=sensi_index/(length(pid_q)*N))
sensi_i<- sensi_i %>% group_by(COUNTRY, round) %>% mutate(nonsensi_index=nonsensi_index/(length(id_q)*N))
sensi_i$censor_index<-sensi_i$sensi_index-sensi_i$nonsensi_index
sensi_i$country_year<-paste0(sensi_i$COUNTRY," - Round ", sensi_i$round)

sensi_i <- sensi_i %>% filter(COUNTRY!="Gabon") %>% filter(COUNTRY!="São Tomé and Príncipe")


ggplot(sensi_i, aes(y=factor(round), x=censor_index, color=factor(round))) + 
  geom_point(size=2.5)+ xlim(0, .23)+
  xlab("Self-censorship Index (R5-7), \nPID vs. Identity Qs") + 
  facet_wrap(~COUNTRY) +
  scale_color_manual(values=c("#69b3a2", "purple", "black"), guide = guide_legend(reverse = TRUE)) + ylab("") + 
  scale_shape(solid = TRUE)+ theme_bw() + theme(text=element_text(size=15),axis.text.y=element_blank(), 
                                                axis.title.y =element_blank(), axis.ticks.y=element_blank(),legend.title=element_blank()) 
# Figure A9: Self-censorship (violence) index by country (R5-7) -----------

sensi_v<-afro %>% group_by(COUNTRY, round) %>% summarise_at(c(pid_q, violence_q, "N"), sum, na.rm=T) 
sensi_v$sensi_index<- rowSums(sensi_v[pid_q], na.rm=T)
sensi_v$nonsensi_index<- rowSums(sensi_v[violence_q], na.rm=T)
sensi_v<- sensi_v %>% group_by(COUNTRY, round) %>% mutate(sensi_index=sensi_index/(length(pid_q)*N))
sensi_v<- sensi_v %>% group_by(COUNTRY, round) %>% mutate(nonsensi_index=nonsensi_index/(length(violence_q)*N))
sensi_v$censor_index<-sensi_v$sensi_index-sensi_v$nonsensi_index
sensi_v$country_year<-paste0(sensi_v$COUNTRY," - Round ", sensi_v$round)

sensi_v <- sensi_v %>% filter(COUNTRY!="Gabon")

ggplot(sensi_v, aes(y=factor(round), x=censor_index, color=factor(round))) + 
  geom_point(size=2.5)+ xlim(0, .23)+
  xlab("Self-censorship Index (R5-7), \nPID vs. Violence Qs") + 
  facet_wrap(~COUNTRY) +
  scale_color_manual(values=c("#69b3a2", "purple", "black"), guide = guide_legend(reverse = TRUE)) + ylab("") + 
  scale_shape(solid = TRUE)+ theme_bw() + theme(text=element_text(size=15),  axis.text.y=element_blank(), 
                                                axis.title.y =element_blank(), axis.ticks.y=element_blank(),
                                                legend.title=element_blank()) 
# Table A10: Qual data Demographic summary statistics --------

# Requires dataset "Full_Qual_Demographic_Data.csv"
qd2<-read.csv("Full_Qual_Demographic_Data.csv")

vars<-c("yop", "age", "femme", "edu2", "news_index", "pid_reveal","pid_refuse", "govt")

var.names<-c("Yopougon","Age", "Female", "Higher ed","News index",  "Feels close to party", "Refused PID", "Govt supporter (RDR or RHDP)"  )

summary_stats<-t(stat.desc(qd2[vars]))

summary_stats<-as.data.frame(summary_stats[,c("mean", "min", "max", "std.dev", "nbr.val")])

summary_stats$desc<-var.names
summary_stats<-summary_stats[c(6,1,2,3,4,5)]
colnames(summary_stats)<-c("description", "mean", "min", "max", "std.dev", "n")

summary_stats 


# Table A11: Qual data Substantive questions ------------------------------

qd<-read.csv("Qualitative_Interview_Data.csv")
qd %>% summarise(across(c(pid1_comf2:others_honest_unaff, pol_polarized:others_trust), mean, na.rm=T)) %>% pivot_longer(pid1_comf2:others_trust) %>% 
  mutate(name=c("How comfortable when asked PID", "Anticipated PID follow-up", "How comfortable when asked party", "Comfort - Incumbent",
                "Comfort - Opposition", "Comfort - unaffiliated","Honest - Incumbent", "Honest - Opposition", "Honest - unaffiliated", 
                "CIV is politically polarized", "CIV is societally polarized", "Trust anonymity", "Others trust anonymity" )) %>% mutate_if(is.numeric, round, 2)  

# Table A12: Why did you feel uncomfortable answering these questi --------

qd %>% filter(pid1_comf<4) %>% mutate(private_inapp=ifelse(pid_comf_1==1 & pid_comf_3==1, 1, 0)) %>%
  summarise(across(c(pid_comf_1:pid_comf_7, private_inapp), mean, na.rm=T)) %>%
  pivot_longer(pid_comf_1:private_inapp) %>% mutate(name=c("PID is private", "Concerned about info use", "Culturally inappropriate", 
                                                           "Other", "Private AND inappropriate")) %>% mutate_if(is.numeric, round, 2)

print(qd$pid_comf_7_TEXT[qd$pid_comf_7_TEXT!=""])

# Table A13: Under which of the following contexts would this ques --------


qd %>% summarise(across(context_1:context_6, mean, na.rm=T)) %>% 
  pivot_longer(context_1:context_6) %>% mutate(name=c("Around elections", "After episodes of violence", 
                                                      "No privacy", "Enumerator different ethnicity", 
                                                      "Enumerator isn't trusted", "Never")) %>% mutate_if(is.numeric, round, 2) 



# Table A14: Bivariate Regression: Polarization and insecurity measures --------------------------------

ivs_insecurity<-c("v2x_clphy","v2caviol")
lapply(vdem[ivs_insecurity], function(x){tidy(felm(v2cacamps_ord~x|year, vdem))}) %>% bind_rows() %>% filter(term!="(Intercept)") %>% mutate_if(is.numeric, round, 2) %>% 
  mutate(term=c("physical violence", "political violence")) 
# Table A15: Interaction Model: Who sent us, any political agent ----------

afro<-merge(afro, vdem, by.x=c("COUNTRY", "round"), by.y=c("COUNTRY", "round"))
afro$sent_anypp<-ifelse(afro$sent_govt==1|afro$sent_pp==1, 1, 0)

ivs<-c("sent_anypp","sent_govt", "sent_pp")

require(texreg)
require(stargazer)
require(lfe)
anypp_reg<-felm(refuse_pid~sent_anypp*polarized|round+COUNTRY, afro)

stargazer(anypp_reg, title="Interaction Model: Who sent us, any political agent", 
          dep.var.labels = "Refuse to Respond (PID)",
          covariate.labels = c("Any political agent", "Polarized", "Any political agent * Polarized"),
          
          type = "latex", header = FALSE, style = "apsr")

# Table A16: Interaction Model: Who sent us, govt ----------

govt_reg<-felm(refuse_pid~sent_govt*polarized|round+COUNTRY, afro) 

stargazer(govt_reg, title="Interaction Model: Who sent us, government", 
          dep.var.labels = "Refuse to Respond (PID)",
          covariate.labels = c("Government", "Polarized", "Government * Polarized"),
          
          type = "latex", header = FALSE, style = "apsr")


# Table A17: Interaction Model: Who sent us, other party ----------

pp_reg<-felm(refuse_pid~sent_pp*polarized|round+COUNTRY, afro)


stargazer(pp_reg, title="Interaction Model: Who sent us, other party", 
          dep.var.labels = "Refuse to Respond (PID)",
          covariate.labels = c("Other party", "Polarized", "Other party * Polarized"),
          
          type = "latex", header = FALSE, style = "apsr")

# Table A18: Interaction Model: Others present during interview ----------

present_reg<-felm(refuse_pid~present*polarized|round+COUNTRY, afro)

stargazer(present_reg, title="Interaction Model: Others present during interview", 
          dep.var.labels = "Refuse to Respond (PID)",
          covariate.labels = c("Others present", "Polarized", "Others present * Polarized"),
          
          type = "latex", header = FALSE, style = "apsr")

# Table A19: Interaction Model: Number of weeks before interview that an election occurred ----------

# merge election data with afrobarometer data

elections<-read.csv("Afrobarometer_Election_Data.csv")

afro_g<-merge(afro, elections, by.x=c("COUNTRY", "round"), by.y=c("country", "wave"))

# make sure the date formats match
afro_g$dateintr2<-format(as.Date(afro_g$dateintr,  "%m/%d/%Y"), "%Y-%m-%d")
afro_g$dateintr3<-format(as.Date(afro_g$dateintr,   "%Y-%m-%d"), "%Y-%m-%d")



afro_g<-afro_g %>% mutate(weeks_prior=as.numeric(difftime(dateintr3,prior_elect_date, units=c("weeks"))),
                          weeks_next=as.numeric(difftime(next_elect_date,dateintr3, units=c("weeks"))),
                          country_round=paste0(COUNTRY, round))

before_reg<-felm(refuse_pid~weeks_prior*polarized|round, afro_g)

require(texreg)

stargazer(before_reg, title="Interaction Model: Number of weeks before interview that an election occurred", 
          dep.var.labels = "Refuse to Respond (PID)",
          covariate.labels = c("Weeks since election", "Polarized", "Weeks since election * Polarized"), type = "latex", header = FALSE, style = "apsr")
# Table A20: Interaction Model: Number of weeks after interview that an election occurred ----------
after_reg<-felm(refuse_pid~weeks_next*polarized|round, afro_g)


stargazer(after_reg, title="Interaction Model: Number of weeks after interview that an election occurred", 
          dep.var.labels = "Refuse to Respond (PID)",
          covariate.labels = c("Weeks until election", "Polarized", "Weeks until election * Polarized"), type = "latex", header = FALSE, style = "apsr")

# Appendix F: Cote d'Ivoire context ---------------------------------------

# These figures were created for a different project. Code for the map in Figure A10 can be found here:  
# https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/LP0TZR

# Cote d'Ivoire electoral and census data can be requested from the author: jumdavis@umich.edu


